//
//  Interface.m
//  FlashLoan
//
//  Created by mac on 2017/5/29.
//  Copyright © 2017年 上海翡鹿信息技术服务有限公司. All rights reserved.
//

#import "Interface.h"
#import <AVOSCloud.h>


@interface Interface ()

@property(nonatomic, strong)AFHTTPSessionManager *shareManger;
@property(nonatomic, strong)AFURLSessionManager *shareURLManger;


@end


@implementation Interface

+(Interface *)shareInstance{
    static Interface *_interface = nil;
    static dispatch_once_t onceInterfaceInitialize;
    dispatch_once(&onceInterfaceInitialize, ^{
        _interface = [[Interface alloc]init];
    });
    return _interface;
}


- (instancetype)init {
    self = [super init];
    if (self) {
        _shareManger = [AFHTTPSessionManager manager];
        _shareManger.responseSerializer = [AFJSONResponseSerializer serializer];
        _shareManger.securityPolicy = [AFSecurityPolicy defaultPolicy];
        _shareManger.securityPolicy.allowInvalidCertificates = YES;
        _shareManger.requestSerializer.timeoutInterval = 10.0;
        
        //默认配置  //AFN3.0+基于封住URLSession的句柄
        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        _shareURLManger = [[AFURLSessionManager alloc]initWithSessionConfiguration:configuration];
    }
    return self;
}


#pragma mark --- public method

-(void)downloadWithBaseurl:(NSString *)baseurl urlstring:(NSString *)urlstring progress:(void (^)(NSString *))downloadProgressBlock finished:(void (^)(NSURLResponse *, NSString *, NSString *))finishedBlock{
    
    //远程地址
    NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",baseurl,urlstring]];
    //请求
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    
    // 下载句柄
    NSURLSessionDownloadTask *downloadTask = [self.shareURLManger downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
        NSString *pro = [NSString stringWithFormat:@"%f",1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount];
        downloadProgressBlock(pro);
    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
        
        NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        NSString *path = [cachesPath stringByAppendingPathComponent:response.suggestedFilename];
        return [NSURL fileURLWithPath:path];
        
    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
        NSString *path = filePath.path;
        finishedBlock(response,path,[NSString stringWithFormat:@"%@",error]);
    }];
    
    [downloadTask resume];
}

-(void)requestClassName:(NSString *)classname finished:(void (^)(NSArray *, NSString *))finishedBlock{
    AVQuery *query = [AVQuery queryWithClassName:classname];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        finishedBlock(objects,[NSString stringWithFormat:@"%@",error]);
    }];
}







@end




